// pages/debtorBaseInfo/businessinfo/businessinfo.ts

import { Debtor } from "../../../../../typings/types/debtor";
import { businessInfoAPI } from "../../../../api/debtor";
import { ZCC } from "../../../../utils/BasePage";
import { formateDigit, validStr } from "../../../../utils/util";

interface PageData {
  // items: Debtor.BaseInfoList[]
  info?: Debtor.BusinessInfo,
  name: string

}

new ZCC.RefreshListPage<Debtor.BaseInfoList>({

  processItemsFromInfo(info: Debtor.BusinessInfo): Debtor.BaseInfoList[] {
    const items: Debtor.BaseInfoList[] = [
      { title: '企业名称', val: validStr(info.name), type: "1" },
      { title: '法定代表人', val: validStr(info.operName), type: "1" },
      { title: '成立日期', val: validStr(info.termStart), sectitle: '经营状态', secval: validStr(info.status), type: "2" },
      { title: '注册资本', val: validStr(formateDigit(info.registCapi)) + validStr(info.registCapiDesc), sectitle: '所属行业', secval: validStr(info.industry), type: "2" },
      { title: '所属地域', val: validStr(info.province), type: "1" },
      { title: '统一社会信用代码', val: validStr(info.creditCode), type: "1" },
      { title: '公司类型', val: validStr(info.econKind), sectitle: '登记机关', secval: validStr(info.belongOrg), type: "2" },
      { title: '营业期限开始', val: validStr(info.startDate), sectitle: '营业期限结束', secval: validStr(info.teamEnd), type: "2", isShowLine: true },
      { title: '电话', val: validStr(info.phoneNumber), sectitle: '邮箱', secval: validStr(info.email), type: "2" },
      { title: '企业地址', val: validStr(info.addressText), type: "1" },
      { title: '曾用名', val: validStr(info.originalName), type: "1" },
      { title: '经营范围', val: validStr(info.scope), type: "1" }
    ]
    return items
  },
  getBusinessInfo: async function () {
    this.setRefreshing()
    // wx.showLoading()
    try {
      const res = await businessInfoAPI(this.data.name);
      if (res) {
        const content = res.content
        console.log(content)
        const items = this.processItemsFromInfo(content)
        this.setData({
          info: content,
          items: items
        })
      }
    } finally {
      this.endRefreshing()
      // wx.hideLoading()
    }
  },
  /**
   * 页面的初始数据
   */
  data: <PageData>{
    // items: [],
    info: undefined,
    name: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {
    this.setData({
      name: this.options.name
    })
    this.getBusinessInfo()
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    this.getBusinessInfo()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

})